window.onload = function () {
    document.getElementById("user_list").className = "add_class";
    document.getElementById("attendance").className = "add-nav-active";
    // initialize()
};

function click_ul() {
    if (document.getElementById("jiantou").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
        document.getElementById("organizational_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou").className = "iconfont icon-jiantou-shang";
        document.getElementById("organizational_management_ul").style.display = "none";
    }
}

function click_ul1() {
    if (document.getElementById("jiantou1").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
        document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-shang";
        document.getElementById("gebruikersrol_ul").style.display = "none";
    }
}

function click_ul2() {
    if (document.getElementById("jiantou2").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-xia";
        document.getElementById("SuperSU_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-shang";
        document.getElementById("SuperSU_ul").style.display = "none";
    }
}

function click_ul3() {
    if (document.getElementById("jiantou3").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-xia";
        document.getElementById("process-manage").style.display = "inline-block";
    }else {
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-shang";
        document.getElementById("process-manage").style.display = "none";
    }
}

function initialize(page=1,query_type='',query_value='') {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('get', "/system-setup/user-list?page="+String(page)+"&query_type="+String(query_type)+"&query_value="+String(query_value), true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                window.JSONdetail = JsonDate["data"]["user_li"];
                window.page = page;
                create_table(JsonDate["data"]["count_num"], JsonDate["data"]["user_li"]);
                pagenum(JsonDate["data"]["count_num"], page,query_type,query_value);
            }
        }
    };
}

// 动态创建列表
function create_table(count_num, json_data) {
    console.log(json_data)
    var t_table = document.getElementById("t_table");
    deltr();
    if (json_data.length === 0){
        document.getElementById("img-display").style.display = "inline-block";
    }else {
        document.getElementById("img-display").style.display = "none";
    }
    for (var i=0; i<json_data.length; i++) {
        window.username = json_data[i]['username'];
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");
        var td5 = document.createElement("td");var td6 = document.createElement("td");var td7 = document.createElement("td");
        td.innerHTML = "<input id='"+json_data[i]["id"]+"' class='check_box' type=\"checkbox\">";td.style.background="#f8f9fa";td.style.textAlign = "center";
        td1.innerHTML = json_data[i]["real_name"];td1.title = json_data[i]["real_name"];td1.style.textAlign = "center";
        td2.innerHTML = json_data[i]["username"];td2.title = json_data[i]["username"];td2.style.textAlign = "center";td2.className='username';
        td3.innerHTML = json_data[i]["mobile"];td3.title = json_data[i]["mobile"];td3.style.textAlign = "center";
        td4.innerHTML = json_data[i]["personnel_category"];td4.title = json_data[i]["personnel_category"];td4.style.textAlign = "center";
        td5.innerHTML = json_data[i]["department"];td5.title = json_data[i]["department"];td5.style.textAlign = "center";
        td6.innerHTML = json_data[i]["position"];td6.title = json_data[i]["position"];td6.style.textAlign = "center";
        var user_index = i;
        td7.innerHTML = "<a href='#' onclick='online_tag("+user_index+")' id="+user_index+">查看</a> | <a href='#' onclick='chongzhimima("+json_data[i]["job_number"]+")'>重置密码</a> | <a href='#' onclick='online_tag()'>人事合同</a>";
        td7.title = '详情';td7.style.textAlign = "center";td7.id=i;
        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);tr.appendChild(td7);
        t_table.appendChild(tr);
    }
}

// 重置密码
function chongzhimima(job_num) {
    document.getElementById("loading").style.display = "inline-block";
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/personal-center/reset_password", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("job_number="+String(job_num));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                alert("重置成功！")
            }
        }
    };
}

// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}

// 加载分页条
function pagenum(dataTotals=0, page=1,query_type,query_value) {
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal:  Math.ceil(parseInt(dataTotals, 10)/20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag:true, //是否显示数据统计
        showSkipInputFlag:true, //是否支持跳转
        getPage: function (page) {
            // var query_type = document.getElementById("query_type").value;
            // var query_value = document.getElementById("query_value").value;
            // initialize(page,query_type,query_value);
            document.getElementById("check_all").checked = false;
        }
    });
}

// 全选与反全选
function check_all_not() {
    var check_box_list = document.getElementsByClassName("check_box");
    if (document.getElementById("check_all").checked) {
        for (var t=0; t<check_box_list.length; t++) {
            check_box_list[t].checked = true;
        }
    }else {
        for (var j=0; j<check_box_list.length; j++) {
            check_box_list[j].checked = false;
        }
    }
}

function update_user() {
    document.getElementById("loading").style.display='inline-block';
    var form = new FormData();
    var real_name = document.getElementById("real_name").value;
    var username = document.getElementById("username").innerHTML;
    // var password = document.getElementById("password").value;
    var department = document.getElementById("department").value;               // 部门
    var gender = document.getElementById("gender").value;               // 性别
    var education = document.getElementById("education").value;         // 学历
    var marital_status = document.getElementById("marital_status").value;               // 婚姻状况
    var political_outlook = document.getElementById("political_outlook").value;     // 政治面貌
    var nation = document.getElementById("nation").value;                                   // 民族
    var ID_number = document.getElementById("ID_number").value;                 // 身份证号
    var native_place = document.getElementById("native_place").value;           // 籍贯
    var major = document.getElementById("major").value;                             // 专业
    var graduated_university = document.getElementById("graduated_university").value;           // 毕业院校
    var position = document.getElementById("position").value;                           // 职称
    var home_address = document.getElementById("home_address").value;                       // 家庭住址
    var mobile = document.getElementById("mobile").value;                                   // 电话
    var mailbox = document.getElementById("mailbox").value;                             // 邮箱
    var remarks = document.getElementById("remarks").value;                     // 备注说明
    // var signature_picture = document.getElementById("signature_picture").files[0];
    // var enclosure = document.getElementById("enclosure").files[0];

    var xhr=new XMLHttpRequest();
    var local_host = window.location.host;
    xhr.open('post', "/system-setup/user-list", true);
    form.append('real_name', String(real_name));
    form.append('username', String(username));
    // form.append('password', String(password));
    form.append('department', String(department));
    form.append('gender', String(gender));
    form.append('education', String(education));
    form.append('marital_status', String(marital_status));
    form.append('political_outlook', String(political_outlook));
    form.append('nation', String(nation));
    form.append('ID_number', String(ID_number));
    form.append('native_place', String(native_place));
    form.append('major', String(major));
    form.append('graduated_university', String(graduated_university));
    form.append('position', String(position));
    form.append('home_address', String(home_address));
    form.append('mobile', String(mobile));
    form.append('mailbox', String(mailbox));
    form.append('remarks', String(remarks));
    // form.append('signature_picture', signature_picture);
    // form.append('enclosure', enclosure);

    xhr.send(form);
    xhr.onreadystatechange=function(){
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display='none';
                var JsonDate = JSON.parse(xhr.responseText);
                if (JsonDate["code"] === 200){
                    alert("信息修改成功！");
                    close_models();
                    console.log(window.page)
                    // initialize(window.page);
                }
                else {
                    alert(JsonDate["message"])
                }
            }
        }
    }
}

function view_details(user_index) {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/select_user_data", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("id="+String(user_index));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                console.log(JsonDate)
                document.getElementById("real_name").value = JsonDate["data"]['real_name']
                document.getElementById("username").innerHTML = JsonDate["data"]['username']
                document.getElementById("department").value = JsonDate["data"]['department']
                document.getElementById("gender").innerText = JsonDate["data"]['gender']
                document.getElementById(JsonDate["data"]["marital_status"]).selected = true
                document.getElementById("education").value = JsonDate["data"]['education']
                document.getElementById("political_outlook").value = JsonDate["data"]['political_outlook']
                document.getElementById("nation").value = JsonDate["data"]['nation']
                document.getElementById("ID_number").value = JsonDate["data"]['ID_number']
                document.getElementById("native_place").value = JsonDate["data"]['native_place']
                document.getElementById("major").value = JsonDate["data"]['major']
                if (JsonDate["data"]["jon_degree"]) {
                    document.getElementById(JsonDate["data"]["jon_degree"]).selected = true
                }
                document.getElementById("graduated_university").value = JsonDate["data"]['graduated_university']
                document.getElementById("position").value = JsonDate["data"]['position']
                document.getElementById("home_address").value = JsonDate["data"]['home_address']
                document.getElementById("mobile").value = JsonDate["data"]['mobile']
                document.getElementById("mailbox").value = JsonDate["data"]['mailbox']
                document.getElementById("remarks").value = JsonDate["data"]['remarks']
                document.getElementById("signature_picture_url_img").src = JsonDate["data"]['signature_picture'];
            }
        }
    };
}


function query_info() {
    var query_type = document.getElementById("query_type").value;
    var query_value = document.getElementById("query_value").value;
    // initialize(1,query_type, query_value)

}


